﻿using DetectingKinectSencor._40_Tools;
using DetectingKinectSencor._50_Evaluation;
using DetectingKinectSencor._50_Evaluation.ViewModels.Dashboard;
using DetectingKinectSencor.Tools;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Shapes;

using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OxyPlot;
using System.Windows.Navigation;
using WPFPageSwitch;
using DetectingKinectSencor;
using DetectingKinectSencor._10_ColorStream;
using DetectingKinectSencor._50_Evaluation.ViewModels.DetailedEvalaution;

namespace WPFPageSwitch
{
    public partial class SlidesPage : ISwitchable
	{
        private SQLiteConnection databaseConnection;
        private string databaseName { get; set; }
        private DateTime lastElementCaptureTime;
        SnapshotManager snapshotManager;
        public TrackingSequences savedCapturesList { get; set; }

        private SlidesViewModelDetailed slidesChangeViewModelDetailed { get; set; }


        public SlidesPage(String databaseName)
		{
            this.databaseName = databaseName;
			InitializeComponent();

            snapshotManager = new SnapshotManager();
            SnapshotImageElement.DataContext = snapshotManager;
		}


        public void initializeSlides()
        {
            // Open DB connection
            databaseConnection = SqliteConnection.openDBConnection(databaseName);

            // Loads the list with the entrys when the tracking was started and returns this list
            savedCapturesList = EvaluationData.loadList(databaseConnection);

            if (savedCapturesList.startCapture.Count == 0)
            {
                MessageBox.Show("There is no sequence in the DB found!");
                return;
            }
            else
            {
                // In the initialization the last element of the capture list will be previewed
                // -1 because the index starts at 0
                lastElementCaptureTime = savedCapturesList.startCapture[savedCapturesList.startCapture.Count - 1];
            }

            // Update the list in the window
            updateSaveCaptureLST();

            // Get the data from the DB and save them to the static lists
            EvaluationData.loadData(databaseConnection, savedCapturesList.startCapture.Count);

            // Close the DB connection
            SqliteConnection.closeConnection(databaseConnection);

            slidesChangeViewModelDetailed = new SlidesViewModelDetailed(lastElementCaptureTime, snapshotManager);

            SlideChangeGraph.DataContext = slidesChangeViewModelDetailed;
            SlideChangeColumn.DataContext = slidesChangeViewModelDetailed;
            SlideChangePerMinute.DataContext = slidesChangeViewModelDetailed;
        }


        public void updateSaveCaptureLST()
        {
            foreach (DateTime startingTime in savedCapturesList.startCapture)
            {
                savedCapturesLST.Items.Add(startingTime.ToString(Clock.dateTimeFormatWithPoints));
            }

            // Select the last item
            //            savedCapturesLST.SelectedItem = lastElementCaptureTime.ToString();
        }


        private void savedCapturesLST_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lb = (ListBox)sender;

            DateTime selectedDate = Convert.ToDateTime(lb.SelectedItem.ToString());


            if (slidesChangeViewModelDetailed.SlidesChangePlotModelTotalAmount != null)
            {
                Graph.deleteAllLineSeries(slidesChangeViewModelDetailed.SlidesChangePlotModel);
                Graph.deleteAllLineSeries(slidesChangeViewModelDetailed.SlidesChangePlotModelTotalAmount);
                Graph.deleteAllLineSeries(slidesChangeViewModelDetailed.SlidesChangePlotModelSlidesPerMinute);

                slidesChangeViewModelDetailed.LoadDataDetailed(selectedDate, snapshotManager);
                
                SlideChangeGraph.RefreshPlot(true);
                SlideChangeColumn.RefreshPlot(true);
                SlideChangePerMinute.RefreshPlot(true);
            }
        }


        private void GesturesPageBTN_Click(object sender, RoutedEventArgs e)
        {
            GesturesPage gesturesPage = new GesturesPage(databaseName);
            gesturesPage.initializeGesture();
            Switcher.Switch(gesturesPage);
        }

        private void MovementPageBTN_Click(object sender, RoutedEventArgs e)
        {
            MovementPage movementPage = new MovementPage(databaseName);
            movementPage.initializeMovement();
            Switcher.Switch(movementPage);
        }

        private void SlidesPageBTN_Click(object sender, RoutedEventArgs e)
        {
            SlidesPage slidesPage = new SlidesPage(databaseName);
            slidesPage.initializeSlides();
            Switcher.Switch(slidesPage);
        }

        private void ViewingDirectionPageBTN_Click(object sender, RoutedEventArgs e)
        {
            ViewingDirectionPage viewingDirectionPage = new ViewingDirectionPage(databaseName);
            viewingDirectionPage.initializeViewingDirection();
            Switcher.Switch(viewingDirectionPage);
        }


        private void DashboardPageBTN_Click(object sender, RoutedEventArgs e)
        {
            Dashboard dashboard = new Dashboard(databaseName);
            dashboard.initializeDashboard();
            Switcher.Switch(dashboard);
        }

        public void UtilizeState(object state)
        {
            throw new NotImplementedException();
        }
	}
}